package gnippets.domain

import refcode.RefCode

/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 *  <p>Integration Tests for the Gnippet class</p>
 *
 *  @author <a href='mailto:kouphax@gmail.com'>James Hughes</a>
 */
 class GnippetTests extends GroovyTestCase {

    def predefinedGniplet
    def predefinedUser
    
    //=========================================================================
    //
    //                    U N I T   T E S T   S E T   U P
    //
    //=========================================================================
    void setUp(){
    
        /* create a predefined user */
        def predefinedTag = new Tag(name:"predefined")
        
        /* save predefined user */
        if(!predefinedTag.save()){
            throw new Exception("Error in setup method. Save user failed.");
        }
        
        /* create some ref data */
        def refCodeLangGroovy = new RefCode(
            domain:'language',
            value:'Groovy',
            meaning:'Groovy',
            sortOrder:0
        )
        
        def refCodeLangSQL = new RefCode(
            domain:'language',
            value:'SQL',
            meaning:'SQL',
            sortOrder:1
        )        
        
        /* save the ref data */
        if(!(refCodeLangSQL.save() && refCodeLangGroovy.save())){
            throw new Exception("Error in setup method. Save RefData failed.");
        }
        
        /* create a pre defined gniplet */
        predefinedGniplet = new Gniplet(
            description:'Test Gniplet Description',
            code:'Test Gniplet Code',
            language:'Groovy'
        )
        
        /* save predefined gniplet */
        if(!predefinedGniplet.save()){
            throw new Exception("Error in setup method. Save Gniplet failed.");
        }    
        
        /* create a predefined user */
        predefinedUser = new GnippetsUser(
            username:'predefined',
            password:'password',
            realName:'Test User',
            email:'predefined@unittest.com'        
        )
        
        /* save predefined user */
        if(!predefinedUser.save()){
            throw new Exception("Error in setup method. Save user failed.");
        }           
    }
    
    //=========================================================================
    //
    //              T I T L E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================
    
    void testConstraintsTitleNull(){
    
        def testGnippet = new Gnippet(
            // null title
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.title = "Test Gnippet Title"
        
        assert testGnippet.validate() == true
    }
    
    void testConstraintsTitleBlank(){
    
        def testGnippet = new Gnippet(
            title:'',
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.title = "Test Gnippet Title"
        
        assert testGnippet.validate() == true
    }    
    
    //=========================================================================
    //
    //   P R I M A R Y   L A N G U A G E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================    
    
    void testConstraintsPrimaryLanguageCustomValidation1(){
    
        def testGnippet = new Gnippet(
            title:'Test Gnippet Title',
            //primaryLanguage null
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.primaryLanguage = "Groovy"
        
        assert testGnippet.validate() == true
    }       
    
    void testConstraintsPrimaryLanguageCustomValidation2(){
    
        def testGnippet = new Gnippet(
            title:'Test Gnippet Title',
            primaryLanguage: '',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.primaryLanguage = "SQL"
        
        assert testGnippet.validate() == true
    }       
    
    void testConstraintsPrimaryLanguageCustomValidation3(){
    
        def testGnippet = new Gnippet(
            title:'Test Gnippet Title',
            primaryLanguage: 'Invalid Language',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.primaryLanguage = "Groovy"
        
        assert testGnippet.validate() == true
    }        
    
    //=========================================================================
    //
    //         D E S C R I P T I O N   C O N S T R A I N T S   T E S T S
    //
    //========================================================================= 
    
    void testConstraintsDescriptionNull(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            //description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.description = "Test Gnippet Description"
        
        assert testGnippet.validate() == true
    }
    
    void testConstraintsDescriptionBlank(){
    
        def testGnippet = new Gnippet(
            title:'Test Gnippet Title',
            primaryLanguage : 'Groovy',
            description : '',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.description = "Test Gnippet Description"
        
        assert testGnippet.validate() == true
    }       
    
    //=========================================================================
    //
    //               T A G S   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================     
    
    void testConstraintsTagsNull(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            //tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.addToTags(new Tag(name:'groovy'))
        
        assert testGnippet.validate() == true
    }    
    
    void testConstraintsTagsEmpty(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.addToTags(new Tag(name:'groovy'))
        
        assert testGnippet.validate() == true
    }     
    
    void testConstraintsTagsTooLarge(){
    
    
        def tag01 = new Tag(name:'tag01')
        def tag02 = new Tag(name:'tag02')
        def tag03 = new Tag(name:'tag03')
        def tag04 = new Tag(name:'tag04')
        def tag05 = new Tag(name:'tag05')
        def tag06 = new Tag(name:'tag06')
        def tag07 = new Tag(name:'tag07')
        def tag08 = new Tag(name:'tag08')
        def tag09 = new Tag(name:'tag09')
        def tag10 = new Tag(name:'tag10')
        def tag11 = new Tag(name:'tag11')
        
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[
                tag01, tag02, tag03,
                tag04, tag05, tag06,
                tag07, tag08, tag09,
                tag10, tag11
            ],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.tags = [
            tag01, tag02, tag03,
            tag04, tag05, tag06,
            tag07, tag08, tag09,
            tag10
        ]
        
        assert testGnippet.validate() == true
    }        
    
    //=========================================================================
    //
    //            G N I P L E T   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================     
    
    void testConstraintsGnipletsNull(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            //gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.gniplets = [predefinedGniplet]
        
        assert testGnippet.validate() == true
    }    
    
    void testConstraintsGnipletsEmpty(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.gniplets = [predefinedGniplet]
        
        assert testGnippet.validate() == true
    }     
    
    void testConstraintsGnipletsTooLarge(){   
    
        def gniplet01 = new Gniplet(description:'Test01', code:'Test01', language:'SQL')
        def gniplet02 = new Gniplet(description:'Test02', code:'Test02', language:'SQL')
        def gniplet03 = new Gniplet(description:'Test03', code:'Test03', language:'SQL')
        def gniplet04 = new Gniplet(description:'Test04', code:'Test04', language:'SQL')
        def gniplet05 = new Gniplet(description:'Test05', code:'Test05', language:'SQL')
        def gniplet06 = new Gniplet(description:'Test06', code:'Test06', language:'SQL')
        def gniplet07 = new Gniplet(description:'Test07', code:'Test07', language:'SQL')
        def gniplet08 = new Gniplet(description:'Test08', code:'Test08', language:'SQL')
        def gniplet09 = new Gniplet(description:'Test09', code:'Test09', language:'SQL')
        def gniplet10 = new Gniplet(description:'Test10', code:'Test10', language:'SQL')
        def gniplet11 = new Gniplet(description:'Test11', code:'Test11', language:'SQL')
        
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[
                gniplet01, gniplet02, gniplet03,
                gniplet04, gniplet05, gniplet06,
                gniplet07, gniplet08, gniplet09,
                gniplet10, gniplet11                                
            ],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.gniplets = [
            gniplet01, gniplet02, gniplet03,
            gniplet04, gniplet05, gniplet06,
            gniplet07, gniplet08, gniplet09,
            gniplet10
        ]
        
        assert testGnippet.validate() == true
    }  
    
    //=========================================================================
    //
    //            R A T I N G   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================     
    
    void testConstraintsRatingNull(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            //rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.rating = 5
        
        assert testGnippet.validate() == true
    }      
    
    void testConstraintsRatingTooLow(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:0,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.rating = 1
        
        assert testGnippet.validate() == true
    }          
    
    void testConstraintsRatingToHigh(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:6,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == false
        
        testGnippet.rating = 5
        
        assert testGnippet.validate() == true
    }          
    
    //=========================================================================
    //
    //          P O S T   D A T E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================      
    
    void testConstraintsPostDateNull(){
    
        def testGnippet = new Gnippet(
            title:"Test Gnippet Title",
            primaryLanguage : 'Groovy',
            description : 'Test Description',
            tags:[new Tag(name:'groovy')],
            gniplets:[predefinedGniplet],
            rating:5,
            user:predefinedUser
        )
        
        assert testGnippet.validate() == true
        
        testGnippet.postDate = null
        
        assert testGnippet.validate() == false

        testGnippet.postDate = new Date()
        
        assert testGnippet.validate() == true
    }     
}

